package cn.melon.mod.store.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import cn.melon.Helper.DBHelper;
import cn.melon.Helper.LoggerHelper;
import cn.melon.base.IBaseExtractor;
import cn.melon.mod.store.dao.GoodsDao;
import cn.melon.mod.store.dao.ShoppingCartDao;
import cn.melon.mod.store.entity.GoodsTable;
import cn.melon.mod.store.entity.ShoppingCartInfo;
import net.ucanaccess.jdbc.UcanaccessSQLException;

public class ShoppingCartDaoImpl implements ShoppingCartDao {

    public String TAG = LoggerHelper.makeLogTag(ShoppingCartDao.class);
    public Logger cartLogger = LoggerHelper.serverLogger();
	
	
	private DBHelper<ShoppingCartInfo> dbHelper= new DBHelper<ShoppingCartInfo>();
	
	private String student_number;
	
	public ShoppingCartDaoImpl(String student_number) {
		this.student_number = student_number;
	}
	
	/**
	 * 用于实现数据库数据转换模式
	 */
	private IBaseExtractor<ShoppingCartInfo> extractor = new IBaseExtractor<ShoppingCartInfo>() {
		@Override
		public List<ShoppingCartInfo> convertToList(ResultSet rs) {

            if (rs == null) {
                return null;
            }

            List<ShoppingCartInfo> cartList = new ArrayList<ShoppingCartInfo>();
            try {
                while (rs.next()) {
                	ShoppingCartInfo cart = new ShoppingCartInfo();

                	cart.setStudent_number(rs.getString("student_number"));
                	cart.setCartGoodsTable(rs.getString("cartGoodsTable"));

                	cartList.add(cart);
                }

                //rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return cartList;
        }
	};
	
	private ShoppingCartInfo getCart(){
		String sql = null;
		sql = "SELECT * FROM  store_ShoppingCartInfo where student_number= ?";
		String[] paras = new String[] {student_number};
		List<ShoppingCartInfo> cartList = null;
		ShoppingCartInfo cartInfo= null;
		try {
			cartList = dbHelper.sqlQuery(sql, paras, extractor);
		} catch (UcanaccessSQLException e) {
			e.printStackTrace();
		}
		
		if(cartList != null){
			cartInfo = cartList.get(0);
			cartLogger.info(TAG + cartInfo.getStudent_number());
		}
		
		return cartInfo;
	}
	
	@Override
	public GoodsTable getCartTable() {

		ShoppingCartInfo cart = getCart();
		
		if(cart == null){
			cart = new ShoppingCartInfo();
		}
		
		GoodsDao goodsDao = new GoodsDaoImpl();
		GoodsTable goodsTable = new GoodsTable();
		for(int i = 0; i < cart.length(); i ++){
			goodsTable.add(goodsDao.getGoodsById(cart.get(i)));
		}
		
		return goodsTable;
	}

	@Override
	public boolean addGoods(String goodsId) {
		String sql = null;
		ShoppingCartInfo cart = getCart();
		String [] paras = new String[2];
		if(cart == null){
			sql = "INSERT INTO store_ShoppingCartInfo "
	                +   "(student_number, cartGoodsTable)"
	                +   "VALUES "
	                +   "(?, ?)";
			cart = new ShoppingCartInfo();
			cart.setStudent_number(student_number);
			cart.add(goodsId);
			paras[0] = cart.getStudent_number();
			paras[1] = cart.toJsonStr();
		} 
		else{
			sql = "update store_ShoppingCartInfo set "
					+ "cartGoodsTable=? where student_number=?";
			
			paras[0] = cart.toJsonStr();
			paras[1] = cart.getStudent_number();	
		}
		
		return dbHelper.sqlUpdate(sql, paras);
	}

	@Override
	public boolean removeGoods(String goodsId) {
		String sql = null;
		ShoppingCartInfo cart = getCart();
		String [] paras = new String[2];
		if(cart == null){
			cartLogger.info(TAG + "不存在此学生的购物车信息");
			return false;
		}
		
		cart.remove(goodsId);
		
		sql = "update store_ShoppingCartInfo set "
				+ "cartGoodsTable=? where student_number=?";
		
		paras[0] = cart.toJsonStr();
		paras[1] = cart.getStudent_number();
		
		return dbHelper.sqlUpdate(sql, paras);
	}

}
